﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Resources;
using System.Globalization;
using System.Text.RegularExpressions;

namespace atm_qlda
{
    public partial class Withdraw : Form
    {
  
        SqlDataAdapter da;
        DataSet ds;
        CultureInfo culture;
        string id;
        string language;
        string error;
        string error1;
        string error2;
        public Withdraw()
        {
            InitializeComponent();
        }

        private void Withdraw_Load(object sender, EventArgs e)
        {
            
        }

        public void SetLanguage(string cultureName)
        {
            culture = CultureInfo.CreateSpecificCulture(cultureName);
            ResourceManager rm = new ResourceManager("atm_qlda.Lang.MyResource", typeof(Withdraw).Assembly);
            lbheaderut.Text = rm.GetString("bank", culture);
            lbsotien.Text = rm.GetString("rut", culture);
            this.Text = rm.GetString("textRut", culture);
            lbClear.Text = rm.GetString("reset", culture);
            lbEnter.Text = rm.GetString("enter", culture);
            lbdk.Text = rm.GetString("dk", culture);
            error = rm.GetString("errorrut", culture);
            error2 = rm.GetString("errorrut2", culture);
            error1 = rm.GetString("errorrut1", culture);
              

        }


        private void bttclean_Click(object sender, EventArgs e)
        {
            txtamount.Focus();
            txtamount.Clear();
        }

        public delegate void delegateSend(string text, string language);

        public void chuyendl2(string chuoi, string lag)
        {
            this.id = chuoi;
            this.language = lag;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string a = txtamount.Text;
            int b = Int32.Parse(a);
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.AppSettings["conStr"];
            ds = new DataSet();
            String sql = "select * from UserAcc where IDCard= '" + id + "'";
            da = new SqlDataAdapter(sql, conn);
            da.Fill(ds, "acc");
            DataTable dt = new DataTable();
            dt = ds.Tables["acc"];
            DataRow dr = dt.Rows[0];
            string count = dr["Balance"].ToString();
            string idacc = dr["IDAcc"].ToString();
            int balance = Int32.Parse(count);
            string loai = "rutien";
            int date = DateTime.Now.Day;
            int month = DateTime.Now.Month;
            int year = DateTime.Now.Year;
            string datetime = month + "/" + date + "/" + year;
            String sql2 = "select * from Trans where IDAcc= '" + idacc+ "'and Date='"+datetime+"' and Loai='"+loai+"'";
            SqlDataAdapter da2 = new SqlDataAdapter(sql2, conn);
            DataSet ds2 = new DataSet();
            DataTable dt2 = new DataTable();
            da2.Fill(ds2, "withdraw");
            dt2 = ds2.Tables["withdraw"];
            int counttrans = dt2.Rows.Count;
                object ojb = dt2.Compute("Sum(Mount)", null);
                string th = "0"+ojb.ToString();
                int tongtrans = Int32.Parse(th);
            int boi = b % 50000;
            if (boi != 0 || b < 50000)
            {
                MessageBox.Show(error);
                txtamount.Focus();
                txtamount.Clear();

            }

            else
                if (balance < b)
                {
                    MessageBox.Show(error1);
                    txtamount.Focus();
                    txtamount.Clear();

                }
                else
                {
                if (counttrans >= 5 || tongtrans >= 9000000)
                {
                    MessageBox.Show(error2);
                    Choose c = new Choose();
                    delegateSend dele2 = new delegateSend(c.chuyendl);
                    dele2(this.id, this.language);
                    c.Show();
                    c.SetLanguage(language);
                    this.Hide();
                }
                
                else
                {
                    balance = balance - b;
                    dr["Balance"] = balance;
                    da.UpdateCommand = new SqlCommand("update UserAcc set Balance = '" + dr["Balance"] + "' where IDCard = '" + id + "'", conn);
                    da.Update(ds, "acc");
                    String sql3 = "select * from Trans";
                    SqlDataAdapter da3 = new SqlDataAdapter(sql3, conn);
                    DataSet ds3 = new DataSet();
                    DataTable dt3 = new DataTable();
                    da3.Fill(ds3, "trans");
                    dt3 = ds3.Tables["trans"];
                    int transid = dt3.Rows.Count + 1;
                    DataRow newRow = dt3.NewRow();
                    newRow["IDTrans"] = transid;
                    newRow["IDAcc"] = idacc;
                    newRow["Loai"] = loai;
                    newRow["Mount"] = b;
                    newRow["Date"] = datetime;
                    dt3.Rows.Add(newRow);
                    SqlCommand add = new SqlCommand(@"insert into Trans(IDTrans,IDAcc,Loai,Mount,Date) values(@idtrans,@idacc,@loai,@mount,@date)", conn);
                    add.Parameters.Add("@idtrans", SqlDbType.VarChar, 15, "IDTrans");
                    add.Parameters.Add("@idacc", SqlDbType.VarChar, 20, "IDAcc");
                    add.Parameters.Add("@loai", SqlDbType.NVarChar, 20, "Loai");
                    add.Parameters.Add("@mount", SqlDbType.Int, 11, "Mount");
                    add.Parameters.Add("@date", SqlDbType.DateTime, 11, "Date");
                    da3.InsertCommand = add;
                    da3.Update(ds3, "trans");
                    chooseReport cr = new chooseReport();
                    delegateSend dele = new delegateSend(cr.chuyendl3);
                    dele(this.id, this.language);
                    cr.Show();
                    cr.SetLanguage(language);
                    this.Hide();
                }
            }
        }


        private void btt50_Click(object sender, EventArgs e)
        {
            txtamount.Text = "50000";
        }

        private void btt100_Click(object sender, EventArgs e)
        {
            txtamount.Text = "100000";
        }

        private void btt1000_Click(object sender, EventArgs e)
        {
            txtamount.Text = "1000000";
        }

        private void bttmax_Click(object sender, EventArgs e)
        {
            txtamount.Text = "2000000";
        }

     
    }
}
 
